from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip

def add_subtitle_to_video(video_path, subtitle_text, output_path):
    # 加载视频
    video = VideoFileClip(video_path)

    # 创建字幕文本剪辑
    subtitle = TextClip(subtitle_text, fontsize=30, color='white', font='Arial', bg_color='black')

    # 将字幕剪辑添加到视频上
    video_with_subtitle = CompositeVideoClip([video, subtitle.set_position(("center", "bottom")).set_duration(video.duration)])

    # 保存带有字幕的视频
    video_with_subtitle.write_videofile(output_path)

# 示例用法
video_path = "./folder/1-声明素材.mp4"
subtitle_text = "这是中文字幕"
output_path = "output_with_subtitle.mp4"

add_subtitle_to_video(video_path, subtitle_text, output_path)